<?php defined("MAIN_DIR") or die("No direct access");

$add_error = false;
$cur_error = false;
if (isset($_POST['action']) && $_POST['action'] == "add")
{
	if (!isset($_POST['RUB']) && !isset($_POST['USD']) && !isset($_POST['EUR']))
		$cur_error = true;
	$bank = filter_var($_POST['bank'],FILTER_SANITIZE_MAGIC_QUOTES);
	$regionally = $route->POST('regionally') ? 1 : 0;
	if ($db->getCell("select `parent` from `bank` where `bank_id` = '$bank' limit 1")) {
		$regionally = 0;
	}
	$name = trim(filter_var($_POST['name'],FILTER_SANITIZE_MAGIC_QUOTES));
	$credit_purpose = trim(filter_var($_POST['credit_purpose'],FILTER_SANITIZE_MAGIC_QUOTES));
	$postponement = trim(filter_var($_POST['postponement'],FILTER_SANITIZE_MAGIC_QUOTES));
	$request_reriod = trim(filter_var($_POST['request_reriod'],FILTER_SANITIZE_MAGIC_QUOTES));
	$material_security = trim(filter_var($_POST['material_security'],FILTER_SANITIZE_MAGIC_QUOTES));
	$surety = trim(filter_var($_POST['surety'],FILTER_SANITIZE_MAGIC_QUOTES));
	$output_form = trim(filter_var($_POST['output_form'],FILTER_SANITIZE_MAGIC_QUOTES));
	$repayment_mode = trim(filter_var($_POST['repayment_mode'],FILTER_SANITIZE_MAGIC_QUOTES));
	$business_age = trim(filter_var($_POST['business_age'],FILTER_SANITIZE_MAGIC_QUOTES));
	$info = trim(filter_var($_POST['info'],FILTER_SANITIZE_MAGIC_QUOTES));
	$add_query = "insert into `business` (`bank_id`,`regionally`,`name`,`credit_purpose`,`postponement`,`request_reriod`,`material_security`,`surety`,`output_form`,`repayment_mode`,`business_age`,`info`) values ('$bank','$regionally','$name','$credit_purpose','$postponement','$request_reriod','$material_security','$surety','$output_form','$repayment_mode','$business_age','$info')";
	if (!$cur_error)
	{
		if (mysql_query($add_query))
		{
			$business = mysql_fetch_array(mysql_query("select * from `business` order by `business_id` desc limit 1"));
			
			if (isset($_POST['RUB']))
			{
				mysql_query("insert into `credit_currency` (`credit_id`,`type`,`currency`,`sum_low`,`sum_high`,`period_low`,`period_high`,`rate_low`,`rate_high`) values ('$business[business_id]','5','RUB','$_POST[RUB_sum_low]','$_POST[RUB_sum_high]','$_POST[RUB_period_low]','$_POST[RUB_period_high]','$_POST[RUB_rate_low]','$_POST[RUB_rate_high]')");
			}
			if (isset($_POST['USD']))
			{
				mysql_query("insert into `credit_currency` (`credit_id`,`type`,`currency`,`sum_low`,`sum_high`,`period_low`,`period_high`,`rate_low`,`rate_high`) values ('$business[business_id]','5','USD','$_POST[USD_sum_low]','$_POST[USD_sum_high]','$_POST[USD_period_low]','$_POST[USD_period_high]','$_POST[USD_rate_low]','$_POST[USD_rate_high]')");
			}
			if (isset($_POST['EUR']))
			{
				mysql_query("insert into `credit_currency` (`credit_id`,`type`,`currency`,`sum_low`,`sum_high`,`period_low`,`period_high`,`rate_low`,`rate_high`) values ('$business[business_id]','5','EUR','$_POST[EUR_sum_low]','$_POST[EUR_sum_high]','$_POST[EUR_period_low]','$_POST[EUR_period_high]','$_POST[EUR_rate_low]','$_POST[EUR_rate_high]')");
			}
			to("/admin/credits/business/edit/$business[business_id]");
		} else {
			$add_error = true;
		}
	}
}

require_once ADMIN.HEADFILE;
?>
<script type="text/javascript" src="/files/js/admin/currency.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("#bank").change(function(){
		if ($("#bank option:selected").hasClass("branch")) {
			$("#regionally").attr("disabled","disabled").removeAttr("checked");
		} else {
			$("#regionally").removeAttr("disabled");
		}
	})
	if ($("#bank")[0].selectedIndex < 3)
		$("#bank")[0].selectedIndex = 3;
})
</script>
<div align="left">
<h1>Добавить кредит (Бизнесу)</h1>
<hr /><?php
if ($add_error)
	echo "<p style='color:red'>Не удалось добавить</p>";
if ($cur_error)
	echo "<p style='color:red'>Необходимо указать данные хотя бы для одной валюты</p>";
?><form method='POST'>
<input type='hidden' name='action' value='add' />
<?php require_once ACREDITS."add_credit_currency.php" ?>
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="280px">Название</td>
		<td width="500px"><input style='width:100%' type='text' name='name' value='<?php if (isset($_POST['name'])) echo $_POST['name'];?>' /></td>
	</tr>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank' id='bank'>
				<?php
				$bankslist = $db->getTable("select `bank_id`,`name` from `bank` where `parent` = '0' order by `name`");
				echo "<option disabled='disabled' style='color:gray'>———————————————</option>
						<option disabled='disabled' style='color:gray'>Головные офисы</option>
						<option disabled='disabled' style='color:gray'>———————————————</option>";
				if ($bankslist) {
					
					foreach ($bankslist as $bank) {
						
						echo "<option value='$bank[bank_id]'";
						if ($bank['bank_id'] == $route->POST('bank'))
							echo " selected='selected'";
						echo ">$bank[name]</option>";
					}
				} else {
					echo "<option disabled='disabled' style='color:gray'>В данном городе нет головных офисов</option>";
				}
				$branches = $db->getTable("select `bank_id`,`name` from `bank` where `parent` != '0' and `city_id` = '$_SESSION[city_admin]' order by `name`");
				echo "<option disabled='disabled' style='color:gray'>———————————————</option>
						<option disabled='disabled' style='color:gray'>Филиалы</option>
						<option disabled='disabled' style='color:gray'>———————————————</option>";
				$regional = false;
				if ($branches) {
					
					foreach ($branches as $branch) {
						
						echo "<option class='branch' value='$branch[bank_id]'";
						if ($branch['bank_id'] == $route->POST('bank')) {
							echo " selected='selected'";
							$regional = true;
						}
						echo ">$branch[name]</option>";
					}
				} else {
					echo "<option disabled='disabled' style='color:gray'>В данном городе нет филиалов</option>";
				}
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Межрегионально</td>
		<td colspan='2' align="left">
			<input type='checkbox' name='regionally' id='regionally' <?php
				if ($route->POST('regionally')) {
					echo "checked='checked'";
				}
				if ($regional) {
					echo "disabled='disabled'";
				}
			?> />
		</td>
	</tr>
	<tr>
		<td>Цель кредита</td>
		<td>
			<input style='width:100%' type='text' name='credit_purpose' value='<?php if (isset($_POST['credit_purpose'])) echo $_POST['credit_purpose'];?>' />
		</td>
	</tr>
	<tr>
		<td>Отсрочка начала погашения</td>
		<td>
			<input style='width:100%' type='text' name='postponement' value='<?php if (isset($_POST['postponement'])) echo $_POST['postponement'];?>' />
		</td>
	</tr>
	<tr>
		<td>Срок рассмотрения заявки (дни)</td>
		<td>
			<input style='width:200px' type='text' name='request_reriod' value='<?php if (isset($_POST['request_reriod'])) echo $_POST['request_reriod'];?>' />
		</td>
	</tr>
	<tr>
		<td>Материальное обеспечение</td>
		<td>
			<input style='width:100%' type='text' name='material_security' value='<?php
				if (isset($_POST['material_security']))
					echo $_POST['material_security'];
			?>' />
		</td>
	</tr>
	<tr>
		<td>Поручительство</td>
		<td>
			<input style='width:100%' type='text' name='surety' value='<?php
				if (isset($_POST['surety']))
					echo $_POST['surety'];
			?>' />
		</td>
	</tr>
	<tr>
		<td>Форма выдачи</td>
		<td>
			<input style='width:100%' type='text' name='output_form' value='<?php
				if (isset($_POST['output_form']))
					echo $_POST['output_form'];
			?>' />
		</td>
	</tr>
	<tr>
		<td>Порядок погашения</td>
		<td>
			<input style='width:100%' type='text' name='repayment_mode' value='<?php
				if (isset($_POST['repayment_mode']))
					echo $_POST['repayment_mode'];
			?>' />
		</td>
	</tr>
	<tr>
		<td>Срок существования бизнеса</td>
		<td>
			<input style='width:100%' type='text' name='business_age' value='<?php
				if (isset($_POST['business_age']))
					echo $_POST['business_age'];
			?>' />
		</td>
	</tr>
	<tr valign='top'>
		<td>Инфо</td>
		<td><textarea style='width:100%' rows='5' name='info' id='info'><?php if (isset($_POST['info'])) echo $_POST['info'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td colspan='2'>
			<p><input type='button' id="insert_city" value='Вставить' /> - Вставить название города<br />
			<input type='button' id="insert_city_rod" value='Вставить' /> - Вставить название города в родительном падеже<br />
			<input type='button' id="insert_city_pred" value='Вставить' /> - Вставить название города в предложном падеже<br />
			</p>
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Добавить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>